import os
import sys
from datetime import date, time, timedelta

# 确保项目根目录在Python路径中
sys.path.insert(0, os.path.abspath(os.path.join(os.path.dirname(__file__), '..')))

from flask import Flask
from venue_booking_system.app.models import db
from venue_booking_system.app.models.venue import Venue, VenueTimeSlot
from venue_booking_system.config import Config

def setup_scenario_standalone():
    """
    一个独立的函数，用于在脚本环境中安全地设置数据库场景，避免循环导入。
    """
    # 创建一个临时的、轻量级的Flask应用实例，只用于数据库操作
    app = Flask(__name__)
    app.config.from_object(Config)
    
    # 绑定数据库到这个临时应用
    db.init_app(app)

    with app.app_context():
        # --- 配置 ---
        VENUE_ID = 20
        TARGET_DATE = date.today() + timedelta(days=3)
        DAY_OF_WEEK = TARGET_DATE.isoweekday()

        venue = Venue.query.get(VENUE_ID)
        if not venue:
            print(f"❌ 错误: 找不到ID为 {VENUE_ID} 的场馆。")
            return

        # --- 1. 清理 ---
        print(f"🧹 正在清理场馆 {VENUE_ID} 在星期 {DAY_OF_WEEK} 的所有已有时间段...")
        VenueTimeSlot.query.filter_by(venue_id=VENUE_ID, day_of_week=DAY_OF_WEEK).delete()
        db.session.commit()
        print("✅ 清理完成。")

        # --- 2. 创建 ---
        print("✨ 正在创建新的、不连续的测试时间段 (09:00-10:00 和 14:00-15:00)...")
        slot1 = VenueTimeSlot(
            venue_id=VENUE_ID, day_of_week=DAY_OF_WEEK, start_time=time(9, 0),
            end_time=time(10, 0), price=100.00, status=1
        )
        slot2 = VenueTimeSlot(
            venue_id=VENUE_ID, day_of_week=DAY_OF_WEEK, start_time=time(14, 0),
            end_time=time(15, 0), price=120.00, status=1
        )
        db.session.add_all([slot1, slot2])
        db.session.commit()
        print("✅ 新的测试时间段创建成功。")
        
        # --- 打印关键信息 ---
        print("\n" + "="*35)
        print("🎉 场景设置完成！ 🎉")
        print(f"场馆ID (VENUE_ID) = {VENUE_ID}")
        print(f"目标日期 (TARGET_DATE) = {TARGET_DATE.strftime('%Y-%m-%d')}")
        print("="*35)

if __name__ == "__main__":
    setup_scenario_standalone() 